class Ready extends eui.Component implements  eui.UIComponent {
	public constructor() {
		super();
		this.skinName = 'ReadySkin';
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}
		
	


	private daojishi;
	private defen;
	private ready;
	private gamePage;
	private gameOverAndBegin;
	private sharePage1;
	private choujiangpin;
	private xueAni;
	private channel;
	protected childrenCreated():void
	{
		super.childrenCreated();
		this.playAnimation(this.xueAni,true);
		var music = RES.getRes("Aaron Angello - Our Canon In D_mp3");
		this.channel=music.play();
		let daoshu = new eui.Label();
		daoshu.textColor = 0xffffff;
		daoshu.size = 60;
		daoshu.bold = true;
		daoshu.y = 500;
		daoshu.width = 750;
		daoshu.height = 70;
		daoshu.textAlign = "center";
		daoshu.verticalAlign = "middle";
		this.addChild(daoshu);
		let daoshutext = new eui.Label();
		daoshutext.textColor = 0xffffff;
		daoshutext.size = 60;
		daoshutext.text = "Ready?";
		daoshutext.bold = true;
		daoshutext.y = 400;
		daoshutext.width = 750;
		daoshutext.height = 70;
		daoshutext.textAlign = "center";
		daoshutext.verticalAlign = "middle";
		this.addChild(daoshutext);
		var num = 4;
		var timer = new egret.Timer(1000,4);
		timer.start();
		timer.addEventListener(egret.TimerEvent.TIMER, timerFunc, this);
		function timerFunc(){
			daoshu.text = `${--num}`;
			if(num == 0){
				this.removeChild(daoshu);
			}
		}

		/*
		准备开始游戏
		*/ 
		setTimeout(function() {
			daoshutext.text = 'GO!';
		}.bind(this), 4000);


		/*
		背景图切换
		*/ 
		setTimeout(function() {
			this.removeChild(daoshutext);
			this.gamePage.visible = true;
		}.bind(this), 4500);


		/*
		开始游戏
		*/
		setTimeout(function() {
			/*
			左右移动
			*/
			

			var mapResult = new egret.Bitmap();
			mapResult.texture = RES.getRes('lu02_png');
			mapResult.width = 90;
			mapResult.height = 90;
			this.addChild(mapResult);

			mapResult.width = 90;
			mapResult.height = 90;
			mapResult.anchorOffsetX = mapResult.width>>1;
			mapResult.x = this.stage.stageWidth>>1;
			mapResult.y = this.stage.stageHeight - mapResult.height;

			var now;
			var last = this.stage.stageWidth>>1;
			var juli;	
			this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,function(e:egret.TouchEvent){

				now = e.stageX;
				mapResult.x = e.stageX;
				juli = now-last;
				juli>0?mapResult.texture = RES.getRes('lu02_png'):mapResult.texture = RES.getRes('lu01_png');
				last = e.stageX;
			},this);
			this.stage.addEventListener(egret.TouchEvent.TOUCH_END,function(e:egret.TouchEvent){

			},this);

			
			/*
			生成多个物品
			*/
			var that = this;
			var is;
			var defenxx = 0;
			/*
			生成
			*/
			var fenshu = new eui.Label();
			var downTimes = setInterval(function(){
				var that = this.stage;
				var img = ["wuping1_png","wuping2_png","wuping10_png","wuping7_png","wuping5_png","wuping6_png","wuping4_png","wuping11_png","wuping3_png","sock——png"];
				var	sprite = new egret.Sprite();
				this.addChild(sprite);
				var	result = new egret.Bitmap();
				var random = Math.floor(Math.random()*10);
				var	imgName = img[random]; 
				result.texture = RES.getRes(imgName);
				sprite.width = 100;
				sprite.height = 100;
				sprite.anchorOffsetX = sprite.width>>1;
				sprite.anchorOffsetY = sprite.height;
				sprite.x = Math.random()*650;
				sprite.y = -sprite.width;
				sprite.addChild(result);
				let isHit = false;
				this.addEventListener( egret.Event.ENTER_FRAME, function( evt:egret.Event ){
					sprite.y += 13;
					var _imgName = imgName;
					var shoujiLeft = mapResult.x;
					var shoujiWidth = mapResult.width;
					var shoujiRight = shoujiLeft+shoujiWidth;
					var shoujiTop = mapResult.y;
					var _that = this;
					if((shoujiLeft<sprite.x+sprite.width && shoujiLeft>sprite.x && shoujiTop<=sprite.y+sprite.height && shoujiTop>=sprite.y+sprite.height-30) || (shoujiRight>sprite.x && shoujiRight<sprite.x+sprite.width && shoujiTop<=sprite.y+sprite.height && shoujiTop>=sprite.y+sprite.height-30)){
						if(!isHit){
							console.log(_imgName);
							if(_imgName == "wuping10_png" || _imgName == "wuping11_png"){
								defenxx = defenxx-1;
							}else{
								defenxx = defenxx+1;
							}
							//++defenxx;
							
							isHit = true;
							if(this.contains(sprite)){
								egret.Tween.get(sprite).to({scaleX:0,scaleY:0},300).call(function(){
									_that.removeChild(sprite);
								});

							}
						}			
					}			
				}, this); 
			}.bind(this),800);

			setTimeout(function(){
				setInterval(function(){
					console.log(defenxx);
					let defen = defenxx*100;
					fenshu.textColor = 0xffffff;
					fenshu.size = 35;
					fenshu.bold = true;
					fenshu.x = 580;
					fenshu.y = 25;
					fenshu.width = 100;
					fenshu.height = 30;
					fenshu.textAlign = "center";
					fenshu.verticalAlign = "middle";
					this.addChild(fenshu);
					fenshu.text = `${defen}`;
				}.bind(this),800);
			}.bind(this),1000);
			
			

			/*
			倒计时
			*/
			let text = new eui.Label();
			text.textColor = 0xffffff;
			text.size = 35;
			text.text = "10";
			text.bold = true;
			text.x = 345;
			text.y = 30;
			text.width = 60;
			text.height = 30;
			text.textAlign = "center";
			text.verticalAlign = "middle";
			this.addChild(text);
			var num = 11;
			var daojishiTime = new egret.Timer(1000,11);
			daojishiTime.start();
			daojishiTime.addEventListener(egret.TimerEvent.TIMER, daojishi, this);
			function daojishi(){
				text.text = `${--num}`;
				if(num == 0){
					
					clearInterval(downTimes);
					setTimeout(function() {
						this.removeChild(mapResult);
						this.sharePage1.visible = true;
						this.gamePage.visible = true;
						let textInput01=new eui.TextInput;
						textInput01.prompt="请输入真实姓名";
						textInput01.skinName="skins.TextInputSkin01";
						textInput01.width=424;
						textInput01.height=62;
						textInput01.x=210;
						textInput01.y=584;
						this.addChild(textInput01);
						let textInput02=new eui.TextInput;
						textInput02.prompt="请输入真实联系方式";
						textInput02.skinName="skins.TextInputSkin02";
						textInput02.width=282;
						textInput02.height=62;
						textInput02.x=210;
						textInput02.y=681;
						this.addChild(textInput02);
						let textInput03=new eui.TextInput;
						textInput03.prompt="请输入收到的短信验证码";
						textInput03.skinName="skins.TextInputSkin03";
						textInput03.width=387;
						textInput03.height=62;
						textInput03.x=247;
						textInput03.y=774;
						this.addChild(textInput03);
						this.choujiangpin.addEventListener(egret.TouchEvent.TOUCH_END,function(){
							
							SceneManages.getInstance(this.stage).loadSence('SlotMachineLotteryDraw',SlotMachineLotteryDraw);
							this.channel.stop();
							this.channel = null;
						}.bind(this),this);
					}.bind(this), 4000);
				}
			}
		}.bind(this), 5000);
	}	
	private playAnimation(target: egret.tween.TweenGroup, isLoop: boolean): void {
		if (isLoop) {
			for (var key in target.items) {
				target.items[key].props = { loop: true};
			}
		}
		target.play();
	}	
}